﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AgileTimeTracker.Client.Wpf.Common;
using AgileTimeTracker.Client.Wpf.TimeControlModule;
using Microsoft.Win32;

namespace AgileTimeTracker.Client.Wpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool isMouseOverTopMostButton = false;
        private double opacityStep = 0.05;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = MainWindowViewModel.Instance;
            this.mainViewContentPresenter.DataContextChanged += mainViewContentPresenter_DataContextChanged;
            this.topMostButton.MouseEnter += topMostButton_MouseEnter;
            this.topMostButton.MouseLeave += topMostButton_MouseLeave;            
            this.PreviewMouseWheel += MainWindow_PreviewMouseWheel;
            this.quitButton.Click += quitButton_Click;
            this.topMostButton.Click += topMostButton_Click;
            this.shrinkButton.Click += shrinkButton_Click;            
        }
        
        void shrinkButton_Click(object sender, RoutedEventArgs e)
        {
            if(MainWindowViewModel.Instance.CurrentView is WorkViewModel)
            {
                var workViewModel = MainWindowViewModel.Instance.CurrentView as WorkViewModel;
                workViewModel.ToogleTaskList();
                this.AjustSize();
            }
        }        

        void topMostButton_Click(object sender, RoutedEventArgs e)
        {
            this.Topmost = !this.Topmost;
        }

        void quitButton_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown(0);
        }

        void MainWindow_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (!this.isMouseOverTopMostButton && this.Opacity > 0.6)
            {
                return;
            }

            if (e.Delta < 0)
            {
                if (this.Opacity >= this.opacityStep)
                {
                    var op = this.Opacity;
                    op -= this.opacityStep;
                    if (op < 0.0)
                    {
                        op = this.opacityStep;
                    }

                    this.Opacity = op;
                }
            }
            else if (this.Opacity <= 1)
            {
                var op = this.Opacity;
                op += this.opacityStep;
                if (op > 1.0)
                {
                    op = 1.0;
                }

                this.Opacity = op;
            }
        }

        void topMostButton_MouseLeave(object sender, MouseEventArgs e)
        {
            this.isMouseOverTopMostButton = false;
        }

        void topMostButton_MouseEnter(object sender, MouseEventArgs e)
        {
            isMouseOverTopMostButton = true;
        }

        void mainViewContentPresenter_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var control = this.mainViewContentPresenter.Content as Control;
            this.AjustSize();
        }

        private void AjustSize()
        {
            this.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
            this.UpdateLayout();               
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            this.DragMove();
        }
    }
}
